<?php

namespace app\models\search;

use app\models\CrmHwzx;
use app\models\User;
use yii\base\Model;
use yii\data\ActiveDataProvider;

class CrmHwzxSearch extends \app\models\CrmHwzx
{
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['gtjl','khbz','khzl','wcj','zxcp','hydj','khly','sfcsqx','khxq','xxdz','gxsj','gsid','mtly','dlxx','zryy','khxb','nld','zy','ldhm','bjhm','khmc','sjhm','jtdh','gzdh','zhld','sfxx','csxx','qxxx','dhfsj','khjf'], 'safe'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * @param array $传递的参数，数组
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = CrmHwzx::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        //设置前端传来的需要查询的数据
        $this->load($params,"CrmHwzxSearch");
        if (!$this->validate()) {
            return $dataProvider;
        }
        //以下是多表联合查询
        $query->select("c.*,u.username,y.zxhm,y.yggh,y.ygmc")
            ->from("crm_hwzx as c")
            ->leftJoin("user as u","u.id=c.gsid")
            ->leftJoin("crm_system_ygxx as y","y.user_id=c.gsid")
            ->andFilterWhere(['like', 'y.yggh', is_set(is_set($_POST,'CrmHwzxSearch'),'yggh')]);
        //是否分配
        switch (is_set(is_set($_POST,'CrmHwzxSearch'),'sffp')){
            case "1"://待分配
                $query->andWhere(['gsid'=>null]);
                break;
            case "2"://已分配
                $query->andFilterWhere(['>','gsid','0']);
                break;
            case "0"://全部
                break;
        }
        //是否成交
        switch (is_set(is_set($_POST,'CrmHwzxSearch'),'sfcj')){
            case "1"://未成交
                break;
            case "2"://已成交
                break;
            case "0"://全部
                break;
        }
        //时间选择类型
        //时间选择
        if (is_set(is_set($_POST,'CrmHwzxSearch'),'sjlx')!=="" && is_set(is_set($_POST,'CrmHwzxSearch'),'sjqj')!==''){
            $timeZone = is_set(is_set($_POST,'CrmHwzxSearch'),'sjqj');
            switch (is_set(is_set($_POST,'CrmHwzxSearch'),'sjlx')){
                case "0":
                    $query->andFilterWhere(['between', 'zzjxsj',$timeZone[0] ,$timeZone[1]]);
                    break;
                case "1":
                    $query->andFilterWhere(['between', 'xgfpsj',$timeZone[0] ,$timeZone[1]]);
                    break;
            }
        }
        //来电号码
        $query->andFilterWhere(['like', 'ldhm', $this->ldhm])
            ->andFilterWhere(['like', 'zxcp', $this->zxcp])
            ->andFilterWhere(['like', 'mtly', $this->mtly])
            ->andFilterWhere(['=', 'zryy', $this->zryy])
            ->andFilterWhere(['=', 'nld', $this->nld])
            ->andFilterWhere(['=', 'zy', $this->zy])
            ->andFilterWhere(['like', 'sfcsqx', $this->sfcsqx])
            ->andFilterWhere(['like', 'khxb', $this->khxb])
            ->andFilterWhere(['like', 'khxq', $this->khxq])
            ->andFilterWhere(['like', 'khmc', $this->khmc])
            ->andFilterWhere(['like', 'khzl', $this->khzl])
            ->andFilterWhere(['like', 'gtnr', $this->gtnr])
            ->andFilterWhere(['=', 'hydj', $this->hydj])
            ->andFilterWhere(['like', 'khbz', $this->khbz])
            ->andFilterWhere(['like', 'khly', $this->khly])
            ->andFilterWhere(['>', 'dhfsj', $this->dhfsj])
            ->andFilterWhere(['>', 'gxsj', $this->gxsj])
            ->andFilterWhere(['=', 'gsid', $this->gsid])
            ->andFilterWhere(['>=', 'wcj', $this->wcj]);

        return $dataProvider;
    }
    /**
     * 拿到用户，归属员
     */
    public function getUser(){
        return $this->hasOne(User::className(),["id"=>"gsid"]);
    }
}
